/**
 * Firedrop preferences
 * Javascript for the preferences file
 */

var firedrop_options = {
	
	prefs: null,
	lastValidCombination: null,
	
	// startup function, loads the preference watcher.
	startup: function() {
		
		// this.prefs = Components.classes["@mozilla.org/preferences-service;1"]
		// 	.getService(Components.interfaces.nsIPrefService)
		// 	.getBranch("extensions.firedrop.");
		// this.prefs.QueryInterface(Components.interfaces.nsIPrefBranch2);
		// this.prefs.addObserver("", this, false);
		
		// initialize component
		this.myComponent = Components.classes['@foobar/mycomponent;1'].getService().wrappedJSObject;
		
		// loads the current dropbox account
		firedrop_options.loadDropBoxAccount();
	},
	
	shutdown: function() {

	},
	
	// dropbox account status
	// TODO, MAKE IT LOOK NICER
	loadDropBoxAccount: function() {
		if (this.myComponent.getPrefForKey("accesstoken") != "") {
			// Signed in
			document.getElementById("dropLogin").setAttribute("value", "You are already signed in");
			document.getElementById("dropAuthButton").hidden = true;
			document.getElementById("logoutButton").hidden = false;
			document.getElementById("logoutButton").setAttribute("label", "Sign in with a different account");
			//document.getElementById("accesssecret").setAttribute("value", "Username: " + this.prefs.getCharPref("accesstoken"));
			//document.getElementById("accesstoken").setAttribute("value", "Username: " + this.prefs.getCharPref("accesstoken"));
		} else {
			// Not signed in		
			document.getElementById("dropLogin").setAttribute("value", "Please sign in");
			document.getElementById("logoutButton").hidden = true;
			document.getElementById("dropAuthButton").hidden = false;
			document.getElementById("dropAuthButton").setAttribute("label", "Sign into DropBox");	
		}
	},
	
	// Function that drives the authentication process
	dropboxAccount: function() {
		// gets the current browser window
		var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);  
		var win = wm.getMostRecentWindow("navigator:browser");
		var url = "";
		// starts the authorization, fetches the auhentication URL
		url = win.dropbox.getAuthUrl();
		
		// calls function in firedrop to send the user to the proper URL
		win.firedrop.dropboxAuth(url);
		win.focus();
	},
	//Logs the current dropbox user and prompts the user to log in again
	logoutDropboxAccount: function() {
		// gets the current browser window
		var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);  
		var win = wm.getMostRecentWindow("navigator:browser");
		var url = "";
		
		this.myComponent.setPrefForKey("accessecret","");
		this.myComponent.setPrefForKey("accesstoken","");
		
		win.getBrowser().selectedTab = win.getBrowser().addTab("https://www.dropbox.com/logout");
		
		win.openDialog("chrome://firedrop/content/options.xul", "", "chrome", "");
		
		close();
	}
	
	
}

// Install load and unload handlers
window.addEventListener("load", function() { firedrop_options.startup(); }, false);
window.addEventListener("unload", function() { firedrop_options.shutdown(); }, false);